home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / freelist.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  3KB  |  123 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef _FREELIST_H
  30. #define _FREELIST_H
  31.  
  32. #include "std.h"
  33. #include "common.h"
  34. #include "memblock.h"
  35. #include "ptrlist.h"
  36.  
  37. // actual implementation
  38. class COMEXP FreelistPriv {
  39. protected:
  40.   FreelistPriv();
  41.   ~FreelistPriv();
  42.  
  43.   void *getRecord(int typesize, int blocksize, int initialblocksize);
  44.   void freeRecord(void *rec);
  45.  
  46. private:
  47.   int total_allocated;
  48.   typedef unsigned char MBT;
  49.   class FLMemBlock : public MemBlock<MBT> {
  50.   public:
  51.     FLMemBlock(int siz) : MemBlock<MBT>(siz) {
  52.       freelist = getMemory();
  53.       nallocated = 0;
  54.     }
  55.     void *freelist;
  56.     int nallocated;    // # of records assigned from block
  57.   };
  58.   PtrList< FLMemBlock > blocks; // the blocks of mem we alloc from
  59. };
  60.  
  61. // type-safe template
  62. static const int DEF_BLOCKSIZE=250;
  63. template
  64. <class T, int BLOCKSIZE=DEF_BLOCKSIZE, int INITIALBLOCKSIZE=DEF_BLOCKSIZE, int ALIGNMENT=4>
  65. class Freelist : private FreelistPriv {
  66. public:
  67.   // just returns the memory, you have to call constructor manually
  68.   T *getRecord() {
  69.     return static_cast<T *>(FreelistPriv::getRecord(itemsize(), BLOCKSIZE, INITIALBLOCKSIZE));
  70.   }
  71.  
  72.   // creates object via default constructor
  73.   T *newRecord() {
  74.     T *ret = getRecord();
  75. #ifdef FORTIFY
  76. #undef new
  77. #endif
  78.     new(ret) T;
  79. #ifdef FORTIFY
  80. #define new ZFortify_New
  81. #endif
  82.     return ret;
  83.   }
  84.  
  85.   // creates object via 1-parameter constructor
  86.   template<class P1>
  87.   T *newRecord(P1 p1) {
  88.     T *ret = getRecord();
  89. #ifdef FORTIFY
  90. #undef new
  91. #endif
  92.     new(ret) T(p1);
  93. #ifdef FORTIFY
  94. #define new ZFortify_New
  95. #endif
  96.     return ret;
  97.   }
  98.  
  99.   // just frees it, you have to call destructor manually
  100.   void freeRecord(T *record) { FreelistPriv::freeRecord(record); }
  101.  
  102.   // calls delete for you, and frees it
  103.   void deleteRecord(T *record) {
  104.     if (record != NULL) {
  105.       record->~T();
  106.       freeRecord(record);
  107.     }
  108.   }
  109.  
  110.   void deletePtrList(PtrList<T> *list) {
  111.     ASSERT(list != NULL);
  112.     for (int i = 0; i < list->getNumItems(); i++) {
  113.       deleteRecord(list->enumItem(i));
  114.     }
  115.     list->removeAll();
  116.   }
  117.  
  118. protected:
  119.   int itemsize() { return (sizeof(T) + (ALIGNMENT-1)) - (sizeof(T) % ALIGNMENT); }
  120. };
  121.  
  122. #endif
  123.